package org.yawnage.base 
  
{
	import flash.utils.Dictionary;
	import org.yawnage.interfaces.base.IMapperObject;
	 /**
	 * <p>//TODO:To be written</p>
	 * @author Berry Jordaan
	 * @date 2011/09/06 09:40 AM
	 * @copyright Copyright © Berry 2011
	 * @version 1.0.0.0
	 * @description
	 * @history Berry Jordaan on 2011/09/06 09:40 AM: Created
	 */
	public class BaseKeyMapper extends BaseEventDispatcher 
	{
		//protected
		protected var _mapper:Dictionary;
		protected var _mapperList:Vector.<IMapperObject>;

		//Enums
		
		
		//Statics
		
		
		//Public
		
		
		//Private
		
		
		public function BaseKeyMapper() 
		{
			super();
			setClassDescription("org.yawnage.baseKeyMapper");
			_mapper = new Dictionary(true);
			_mapperList = new Vector.<IMapperObject>();
		}
		
		protected function addObject(object:IMapperObject):IMapperObject{
			if (hasObject(object.key)) {
				_trace("ERROR: Duplicate key [" + object.key + "] found. - " + toString(), this, 0xFF0000 );
				return _mapper[object.key];
			}
			_mapper[object.key] = object;
			_mapperList.push(_mapper[object.key]);
			return _mapper[object.key];
		}
		
		protected function removeObject(key:String):Boolean{
			if (!hasObject(key)) {
				_trace("ERROR: No such object [" + key + "] found. - " + toString(), this, 0xFF0000 );
				return false;
			}
			
			var index:int = _mapperList.indexOf( _mapper[key] );
			_mapperList.splice(index, 1);
			_mapper[key].dispose();
			_mapper[key] = null;
			delete _mapper[key];
			return true;
		}
		
		protected function removeAllObjects():void{
			
			for each(var obj:IMapperObject in _mapper) {
				obj.dispose();
				removeObject(obj.key);
			}
			_trace("NOTICE: All objects was removed. -" + toString(), this, 0xC0C0C0 );
		}
		
		protected function getObjectbyKey(key:String):IMapperObject {
			if (hasObject(key)) {
				return _mapper[key];
			}
			return null;
		}
		
		protected function hasObject(key:String):Boolean {
			return (_mapper[key] != null);
		}
		
		override public function dispose():void {
			removeAllObjects();
			_mapper = null
			super.dispose();
		}

	}

}